package com.demo.studentmanager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.studentmanager.dto.TeacherAddDto;
import com.demo.studentmanager.dto.TeacherListDto;
import com.demo.studentmanager.dto.TeacherUpdDto;
import com.demo.studentmanager.entity.Teacher;
import com.demo.studentmanager.result.ResultCode;
import com.demo.studentmanager.service.IDepartmentService;
import com.demo.studentmanager.service.ITeacherService;
import com.demo.studentmanager.util.CheckEntityUtil;
import com.demo.studentmanager.vo.TeacherVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.validation.constraints.NotNull;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author zjia
 * @date
 */
@RestController
@Api(tags = "教师模块")
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    ITeacherService teacherService;

    @Autowired
    IDepartmentService departmentService;

    @RequiresRoles(value={"teacher","admin"},logical= Logical.OR)
    @GetMapping
    @ApiOperation(value = "查询教师信息")
    public IPage<TeacherVo> getTeacher(@Validated TeacherListDto teacherListDto) {
        return teacherService.getTeacherList(teacherListDto);
    }


    @RequiresRoles({"admin"})
    @PostMapping
    @ApiOperation(value = "增加教师信息")
    public void addTeacher(@Validated TeacherAddDto teacherAddDto) {

        CheckEntityUtil.isNotNullById(teacherAddDto.getTno(), teacherService, ResultCode.TEACHER_ACCOUNT_EXIST);

        CheckEntityUtil.isNullById(teacherAddDto.getDeptno(), departmentService, ResultCode.DEPARTMENT_NOT_EXIST);

        Teacher teacher = new Teacher();
        teacherAddDto.convertToEntity(teacher);

        teacherService.save(teacher);

    }

    @RequiresRoles(value={"teacher","admin"},logical= Logical.OR)
    @PutMapping
    @ApiOperation(value = "修改教师信息")
    public void upTeacher(@Validated TeacherUpdDto teacherUpdDto) {

        Teacher teacher = CheckEntityUtil.isNullById(teacherUpdDto.getTno(), teacherService, ResultCode.TEACHER_ACCOUNT_NOT_EXIST);

        if (teacherUpdDto.getDeptno() != null) {
            CheckEntityUtil.isNullById(teacherUpdDto.getDeptno(), departmentService, ResultCode.DEPARTMENT_NOT_EXIST);
        }

        teacherUpdDto.convertToEntity(teacher);
        teacherService.updateById(teacher);

    }

    @RequiresRoles({"admin"})
    @DeleteMapping("/{tno}")
    @ApiOperation(value = "删除教师信息")
    public void delTeacher(@NotNull @PathVariable String tno) {

        CheckEntityUtil.isNullById(tno, teacherService, ResultCode.TEACHER_ACCOUNT_NOT_EXIST);

        teacherService.removeById(tno);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //是否严格解析时间 false则严格解析 true宽松解析
        dateFormat.setLenient(false);
        // CustomDateEditor为自定义日期编辑器
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

}
